import re
import itchat

#登入并保存登入状态，实现第一次运行时扫码，一定时间内再次运行就不用扫码了，手机微信上将显示：网页微信已登入.....
import os
import pandas as pd
#
# filepath = "temp.txt"
# for line in open(filepath,'rb'):
#     content = line.decode()
# if not os.path.exists(filepath) or not content:
#     # itchat.auto_login(hotReload=True)
#     itchat.login()
#     #发送文本数据到文件助手
#     # itchat.send("东小东你好123",toUserName="filehelper")
#     friends = itchat.get_friends(update=True)
#     df_friends = pd.DataFrame(friends)
#     City = df_friends.City
#     City_count = City.value_counts()
#     City_count = City_count[City_count.index != ''] #过滤地理位置为空的好友
#     print(type(City_count))
#     f = open(filepath,'ab')
#     f.write(str(City_count).encode())
#     print('保存成功！')
#     f.close()
# else:
#     print(content)
# # print(City_count)
# # itchat.logout()  #登出
    # Province = item.Province
    # City = item.City
    # Province_count = Province.value_counts()
    # print(Province_count)
    # NickName = item.NickName
    # NickName_count = NickName.value_counts()
    # print(NickName,Province,City)
from pyecharts import Map, Geo

area = ["广州","韶关","深圳","珠海","汕头","佛山","江门","湛江","茂名","肇庆","惠州","梅州","汕尾","河源","阳江","清远","东莞","中山","潮州","揭阳","云浮"]

def friends_province():
    # 获取好友省份
    province= get_data("Province")
    # 分类
    province_distribution = {}
    for item in province:
        #删除英文省份，因为中国地图表中没有
        if bool(re.search('[a-z]',item)):
            continue
        elif not province_distribution.__contains__(item):
            province_distribution[item] = 1
        else:
            province_distribution[item] += 1
    #将省份名为空的删除
    province_distribution.pop('')
    #提取地图接口需要的数据格式
    # print(province_distribution)
    province_keys=province_distribution.keys()
    province_values=province_distribution.values()
    return province_keys,province_values
def friends_city(name):
    # 获取好友省份
    city= get_data("City")
    city_count = city.value_counts()
    gd_citys = {}
    for name,count in city_count.items():
        if name in area:
            gd_citys[name] = count
    city_keys=gd_citys.keys()
    city_values=gd_citys.values()
    return city_keys,city_values
def get_data(name):
    df_friends = pd.DataFrame(friends)
    return df_friends[name]

name = "广东"
itchat.auto_login(hotReload=True)
friends = itchat.get_friends(update=True)

# city_keys,city_values = friends_city(name) #城市
# map = Geo(name+"地图","",title_color="#fff", title_pos="center",width=800, height=600, background_color='#001119')
# map.add("",city_keys,city_values, visual_range=[0, 50],  maptype=name, is_visualmap=True,
#         symbol_size=15,visual_text_color='#fff',type="effectScatter", is_random=True, effect_scale=5,)# is_roam=False 是否固定
# map.show_config()
# map.render(path="./"+name+".html")

# maptype='china' 只显示全国直辖市和省级
# 数据只能是省名和直辖市的名称
province_keys, province_values= friends_province() #省份
map = Geo("中国地图",'', title_color="#fff", title_pos="center",width=800, height=600, background_color='#001119')
map.add("",province_keys,province_values, visual_range=[0, 50],  maptype='china', is_visualmap=True,
        symbol_size=15, visual_text_color='red',type="effectScatter", is_random=True, effect_scale=5,is_roam=False)
map.show_config()
map.render(path="./中国地图.html")
